/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Xml.XPath;
using DaveSexton.Controls;
using DaveSexton.DocProject.Sandcastle.ContentManagement;

namespace DaveSexton.DocProject.Sandcastle.TopicManagement
{
	public delegate void SaveCommentsCallback(TopicNode node, XmlCommentSectionContentItem section);
	public delegate void LoadTopicNodeCallback(TopicNode node);

	[Serializable]
	public sealed class TopicNode : XmlTreeNode
	{
		#region Public Properties
		public new TopicNode Parent
		{
			get
			{
				return base.Parent as TopicNode;
			}
		}

		public Topic Topic
		{
			get
			{
				return topic;
			}
		}
		#endregion

		#region Private / Protected
		private readonly Topic topic;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="TopicNode" /> class.
		/// </summary>
		internal TopicNode(Topic topic)
			: base(topic.Element)
		{
			if (topic == null)
				throw new ArgumentNullException("topic");

			this.topic = topic;

			Text = topic.TocTitle;

			ReflectionApiElement element = topic.ApiElement;

			string imageKey = null;

			if (element == null)
				imageKey = topic.TopicType.ToString();
			else
			{
				if (!element.IsApi && !element.IsMethodOverloads)
				{
					if (element.TopicType == TopicType.Project)
						imageKey = "Project";
					else
						imageKey = "General";
				}
				else
				{
					if (element.IsApi)
						ToolTipText = element.TopicAccess.ToString() + " " + element.TopicType.ToString();
					else if (element.IsMethodOverloads)
						ToolTipText = "Method Overloads";

					switch (element.TopicAccess)
					{
						case TopicAccessibility.Public:
							imageKey = element.TopicType.ToString();
							break;
						case TopicAccessibility.ProtectedInternal:
							imageKey = element.TopicType.ToString() + "Protected";
							break;
						default:
							imageKey = element.TopicType.ToString() + element.TopicAccess.ToString();
							break;
					}
				}
			}

			ImageKey = imageKey;
			SelectedImageKey = imageKey;
		}

		/// <summary>
		/// Constructs a new instance of the <see cref="TopicNode" /> class.
		/// </summary>
		internal TopicNode(Topic topic, string imageKey)
			: base(topic.Element)
		{
			if (topic == null)
				throw new ArgumentNullException("topic");

			this.topic = topic;

			Text = topic.TocTitle;
			
			ImageKey = imageKey;
			SelectedImageKey = imageKey;
		}

		/// <summary>
		/// Constructs a new instance of the <see cref="TopicNode" /> class by copying the values of the specified <paramref name="clone"/>.
		/// </summary>
		public TopicNode(TopicNode clone)
			: base(clone)
		{
			if (clone == null)
				throw new ArgumentNullException("clone");

			topic = new Topic(clone.topic);
		}

		/// <summary>
		/// Constructs a new instance of the <see cref="TopicNode" /> class for deserialization.
		/// </summary>
		private TopicNode(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
			: base(info, context)
		{
			topic = (Topic) info.GetValue("topic", typeof(Topic));
		}
		#endregion

		#region Methods
		protected override void Serialize(System.Runtime.Serialization.SerializationInfo si, System.Runtime.Serialization.StreamingContext context)
		{
			base.Serialize(si, context);

			si.AddValue("topic", topic);
		}

		public override object Clone()
		{
			return new TopicNode(this);
		}
		#endregion

		#region Event Handlers
		#endregion
	}
}